home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / macros.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  8KB  |  282 lines

  1. /* Keyboard macros.
  2.    Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "macros.h"
  24. #include "commands.h"
  25. #include "buffer.h"
  26. #include "window.h"
  27.  
  28. #ifdef STDC_HEADERS
  29. #include <stdlib.h>
  30. #endif
  31. #include "macros_p.h"
  32. #include "xdisp_p.h"
  33. static Lisp_Object pop_kbd_macro _P_((Lisp_Object info));
  34.  
  35. Lisp_Object Qexecute_kbd_macro;
  36.  
  37. int defining_kbd_macro;
  38.  
  39. /* The start of storage for the current keyboard macro, and its size.  */
  40. Lisp_Object *kbd_macro_buffer;
  41. int kbd_macro_bufsize;
  42.  
  43. /* Where to store the next keystroke of the macro.  */
  44. Lisp_Object *kbd_macro_ptr;
  45.  
  46. /* The finalized section of the macro starts at kbd_macro_buffer and
  47.    ends before this.  This is not the same as kbd_macro_pointer, because
  48.    we advance this to kbd_macro_pointer when a key's command is complete.
  49.    This way, the keystrokes for "end-kbd-macro" are not included in the
  50.    macro.  */
  51. Lisp_Object *kbd_macro_end;
  52.  
  53. Lisp_Object Vlast_kbd_macro;
  54.  
  55. Lisp_Object Vexecuting_macro;
  56. int executing_macro_index;
  57.  
  58.  
  59. DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 1, "P",
  60.   "Record subsequent keyboard input, defining a keyboard macro.\n\
  61. The commands are recorded even as they are executed.\n\
  62. Use \\[end-kbd-macro] to finish recording and make the macro available.\n\
  63. Use \\[name-last-kbd-macro] to give it a permanent name.\n\
  64. Non-nil arg (prefix arg) means append to last macro defined;\n\
  65.  This begins by re-executing that macro as if you typed it again.")
  66.   (append)
  67.      Lisp_Object append;
  68. {
  69.   if (defining_kbd_macro)
  70.     error ("Already defining kbd macro");
  71.  
  72.   update_mode_lines++;
  73.   if (NILP (append))
  74.     {
  75.       kbd_macro_ptr = kbd_macro_buffer;
  76.       kbd_macro_end = kbd_macro_buffer;
  77.       message("Defining kbd macro...");
  78.     }
  79.   else
  80.     {
  81.       message("Appending to kbd macro...");
  82.       kbd_macro_ptr = kbd_macro_end;
  83.       Fexecute_kbd_macro (Vlast_kbd_macro, make_number (1));
  84.     }
  85.   defining_kbd_macro++;
  86.   
  87.   return Qnil;
  88. }
  89.  
  90. DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 1, "p",
  91.   "Finish defining a keyboard macro.\n\
  92. The definition was started by \\[start-kbd-macro].\n\
  93. The macro is now available for use via \\[call-last-kbd-macro],\n\
  94. or it can be given a name with \\[name-last-kbd-macro] and then invoked\n\
  95. under that name.\n\
  96. \n\
  97. With numeric arg, repeat macro now that many times,\n\
  98. counting the definition just completed as the first repetition.\n\
  99. An argument of zero means repeat until error.")
  100.   (arg)
  101.      Lisp_Object arg;
  102. {
  103.   if (!defining_kbd_macro)
  104.       error ("Not defining kbd macro.");
  105.  
  106.   if (NILP (arg))
  107.     XFASTINT (arg) = 1;
  108.   else
  109.     CHECK_NUMBER (arg, 0);
  110.  
  111.   if (defining_kbd_macro)
  112.     {
  113.       defining_kbd_macro = 0;
  114.       update_mode_lines++;
  115.       Vlast_kbd_macro = make_event_array (kbd_macro_end - kbd_macro_buffer,
  116.                       kbd_macro_buffer);
  117.       message("Keyboard macro defined");
  118.     }
  119.  
  120.   if (XFASTINT (arg) == 0)
  121.     Fexecute_kbd_macro (Vlast_kbd_macro, arg);
  122.   else
  123.     {
  124.       XSETINT (arg, XINT (arg)-1);
  125.       if (XINT (arg) > 0)
  126.     Fexecute_kbd_macro (Vlast_kbd_macro, arg);
  127.     }
  128.   return Qnil;
  129. }
  130.  
  131. /* Store character c into kbd macro being defined */
  132.  
  133. _VOID_
  134. store_kbd_macro_char (c)
  135.      Lisp_Object c;
  136. {
  137.   if (defining_kbd_macro)
  138.     {
  139.       if (kbd_macro_ptr - kbd_macro_buffer == kbd_macro_bufsize)
  140.     {
  141.       register Lisp_Object *new
  142.         = (Lisp_Object *) xrealloc (kbd_macro_buffer,
  143.                     ((kbd_macro_bufsize *= 2)
  144.                      * sizeof (Lisp_Object)));
  145.       kbd_macro_ptr += new - kbd_macro_buffer;
  146.       kbd_macro_end += new - kbd_macro_buffer;
  147.       kbd_macro_buffer = new;
  148.     }
  149.       *kbd_macro_ptr++ = c;
  150.     }
  151. }
  152.  
  153. /* Declare that all chars stored so far in the kbd macro being defined
  154.  really belong to it.  This is done in between editor commands.  */
  155.  
  156. _VOID_
  157. finalize_kbd_macro_chars ()
  158. {
  159.   kbd_macro_end = kbd_macro_ptr;
  160. }
  161.  
  162. DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
  163.   0, 1, "p",
  164.   "Call the last keyboard macro that you defined with \\[start-kbd-macro].\n\
  165. \n\
  166. A prefix argument serves as a repeat count.  Zero means repeat until error.\n\
  167. \n\
  168. To make a macro permanent so you can call it even after\n\
  169. defining others, use \\[name-last-kbd-macro].")
  170.   (prefix)
  171.      Lisp_Object prefix;
  172. {
  173.   if (defining_kbd_macro)
  174.     error ("Can't execute anonymous macro while defining one");
  175.   else if (NILP (Vlast_kbd_macro))
  176.     error ("No kbd macro has been defined");
  177.   else
  178.     Fexecute_kbd_macro (Vlast_kbd_macro, prefix);
  179.   return Qnil;
  180. }
  181.  
  182. /* Restore Vexecuting_macro and executing_macro_index - called when
  183.    the unwind-protect in Fexecute_kbd_macro gets invoked.  */
  184. static Lisp_Object
  185. pop_kbd_macro (info)
  186.      Lisp_Object info;
  187. {
  188.   Lisp_Object tem;
  189.   Vexecuting_macro = Fcar (info);
  190.   tem = Fcdr (info);
  191.   executing_macro_index = XINT (tem);
  192.   return Qnil;
  193. }
  194.  
  195. DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 2, 0,
  196.   "Execute MACRO as string of editor command characters.\n\
  197. If MACRO is a symbol, its function definition is used.\n\
  198. COUNT is a repeat count, or nil for once, or 0 for infinite loop.")
  199.   (macro, prefixarg)
  200.      Lisp_Object macro, prefixarg;
  201. {
  202.   Lisp_Object final;
  203.   Lisp_Object tem;
  204.   int count = specpdl_ptr - specpdl;
  205.   int repeat = 1;
  206.   struct gcpro gcpro1;
  207.  
  208.   if (!NILP (prefixarg))
  209.     prefixarg = Fprefix_numeric_value (prefixarg),
  210.     repeat = XINT (prefixarg);
  211.  
  212.   final = indirect_function (macro);
  213.   if (XTYPE (final) != Lisp_String
  214.       && XTYPE (final) != Lisp_Vector)
  215.     error ("Keyboard macros must be strings or vectors.");
  216.  
  217.   XFASTINT (tem) = executing_macro_index;
  218.   tem = Fcons (Vexecuting_macro, tem);
  219.   record_unwind_protect (pop_kbd_macro, tem);
  220.  
  221.   GCPRO1 (final);
  222.   do
  223.     {
  224.       Vexecuting_macro = final;
  225.       executing_macro_index = 0;
  226.  
  227.       command_loop_1 ();
  228.  
  229.       QUIT;
  230.     }
  231.   while (--repeat && (XTYPE (Vexecuting_macro) == Lisp_String
  232.               || XTYPE (Vexecuting_macro) == Lisp_Vector));
  233.  
  234.   UNGCPRO;
  235.   return unbind_to (count, Qnil);
  236. }
  237.  
  238. _VOID_
  239. init_macros ()
  240. {
  241.   Vlast_kbd_macro = Qnil;
  242.   defining_kbd_macro = 0;
  243.  
  244.   Vexecuting_macro = Qnil;
  245. }
  246.  
  247. _VOID_
  248. syms_of_macros ()
  249. {
  250.   kbd_macro_bufsize = 100;
  251.   kbd_macro_buffer = (Lisp_Object *) malloc (kbd_macro_bufsize
  252.                          * sizeof (Lisp_Object));
  253.  
  254.   Qexecute_kbd_macro = intern ("execute-kbd-macro");
  255.   staticpro (&Qexecute_kbd_macro);
  256.  
  257.   defsubr (&Sstart_kbd_macro);
  258.   defsubr (&Send_kbd_macro);
  259.   defsubr (&Scall_last_kbd_macro);
  260.   defsubr (&Sexecute_kbd_macro);
  261.  
  262.   DEFVAR_BOOL ("defining-kbd-macro", &defining_kbd_macro,
  263.     "Non-nil while a keyboard macro is being defined.  Don't set this!");
  264.  
  265.   DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
  266.     "Currently executing keyboard macro (a string); nil if none executing.");
  267.  
  268.   DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
  269.     "Currently executing keyboard macro (a string); nil if none executing.");
  270.  
  271.   DEFVAR_LISP ("last-kbd-macro", &Vlast_kbd_macro,
  272.     "Last kbd macro defined, as a string; nil if none defined.");
  273. }
  274.  
  275. _VOID_
  276. keys_of_macros ()
  277. {
  278.   initial_define_key (control_x_map, ('e'), "call-last-kbd-macro");
  279.   initial_define_key (control_x_map, ('('), "start-kbd-macro");
  280.   initial_define_key (control_x_map, (')'), "end-kbd-macro");
  281. }
  282.